package main

import "fmt"

func main() {
	var runes []rune
	for _, r := range "Hello, 世界" {
		runes = append(runes, r)
	}

	fmt.Printf("%q \n", runes)

	a := []rune("Hello, 世界")

	fmt.Printf("%q \n", a)

	// var x, y []int
	var y []int
	for i := 0; i < 10; i++ {
		// y = appendInt(x, i)
		y = append(y, i)
		fmt.Printf("%d cap=%d\t%v\n", i, cap(y), y)
		// x = y
	}
}

func appendInt(x []int, y int) []int {
	var z []int
	zlen := len(x) + 1
	if zlen <= cap(x) { //没有超出容量
		z = x[:zlen] //多取一个长度
	} else { //超出了容量,需要重新生成slice
		zcap := zlen
		if zcap < 2*len(x) {
			zcap = 2 * len(x) //一次多给一倍的容量,为后续操作留空间
		}
		z = make([]int, zlen, zcap) //生成大容量slice
		copy(z, x)                  //把x值复制到z
	}
	z[len(x)] = y //往z里追加y
	return z
}
